/*
 * 通话
 * page通话
 */
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:qgchat/bloc/blocProvider.dart';
import 'package:qgchat/bloc/msg/callRecordsBloc.dart';
import 'package:qgchat/common/class/date/index.dart';
import 'package:qgchat/common/global/global.dart';
import 'package:qgchat/http/models/callRecordModel.dart';
import 'package:qgchat/widgets/easyRefreshWidget.dart';
import 'package:qgchat/widgets/noData.dart';
import 'package:qgchat/widgets/avatar.dart';

class Call extends StatefulWidget {
  Call({Key key}) : super(key: key);

  @override
  _CallState createState() => _CallState();
}

class _CallState extends State<Call> {
  String token = '';

  // 生成appbar
  Widget appbarHandle() {
    return PreferredSize(
      preferredSize: Size.fromHeight(px(88)),
      child: AppBar(
        centerTitle: true,
        title: Text(
          "通话",
          style: TextStyle(
            color: Colors.black,
            fontSize: px(34),
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    token = state(context, 'user').info.memberToken;
    return Scaffold(
      appBar: appbarHandle(),
      body: BlocProvider(
        bloc: CallRecordsBlog(token),
        child: BodyHandle(),
      ),
    );
  }
}

class BodyHandle extends StatelessWidget {
  // 生成主播列表
  Widget makeAnchorHandle(CallRecordModel item) {
    return Container(
      height: px(160),
      padding: EdgeInsets.symmetric(horizontal: px(30)),
      child: Row(
        children: <Widget>[
          Avatar(
            item.memberHead,
            true,
            token: item.memberToken,
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.only(left: px(23)),
              decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    color: col("edeff2"),
                    width: 1,
                  ),
                ),
              ),
              child: Column(
                children: <Widget>[
                  // 昵称和时间
                  Container(
                    margin: EdgeInsets.only(top: px(35)),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          item.memberNickname,
                          style: TextStyle(
                            height: .95,
                            color: col("303133"),
                            fontSize: px(34),
                          ),
                        ),
                        Text(
                          "${Date.simple(int.parse(item.createtime), format: "y/m/d h:i")}",
                          style: TextStyle(
                            height: .99,
                            color: col("bfc2cc"),
                            fontSize: px(26),
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 金币和通话时间
                  callStatusHandle(item),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  // 生成通话状态
  Widget callStatusHandle(CallRecordModel item) {
    // 时间差计算
    String callDuration(String startTime, String endTime) {
      var st = DateTime.fromMillisecondsSinceEpoch(int.parse(startTime) * 1000);
      var et = DateTime.fromMillisecondsSinceEpoch(int.parse(endTime) * 1000);
      var difference = et.difference(st);
      if (difference.isNegative) {
        return '开始时间大于结束时间';
      } else {
        return '0' + difference.toString().split('.')[0];
      }
    }

    if (item.talktime != '0') {
      return Container(
        margin: EdgeInsets.only(top: px(20)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Container(
              child: Row(
                children: <Widget>[
                  Container(
                    margin: EdgeInsets.only(right: px(10)),
                    child: Image.asset(
                      "assets/images/icon/star.png",
                      width: px(32),
                      height: px(32),
                    ),
                  ),
                  Text(
                    item.gold >= 0 ? '${item.gold}' : item.gold.toString(),
                    style: TextStyle(
                      height: .99,
                      color: col("ff7b43"),
                      fontSize: px(32),
                    ),
                  ),
                ],
              ),
            ),
            Container(
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.query_builder,
                    color: col("717377"),
                    size: px(28),
                  ),
                  Container(
                    margin: EdgeInsets.only(left: px(15)),
                    child: Text(
                      callDuration(item.talktime, item.endtime),
                      style: TextStyle(
                        color: col("606266"),
                        fontSize: px(26),
                      ),
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      );
    } else {
      return Container(
        margin: EdgeInsets.only(top: px(20)),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: <Widget>[
            Container(),
            Text(
              item.status == 1 ? '未接来电' : item.status == 2 ? '已拒绝' : '未接通',
              style: TextStyle(
                fontSize: px(26),
                color: col("ff3b30"),
              ),
            ),
          ],
        ),
      );
    }
  }

  List<Widget> callList(List<CallRecordModel> list) {
    return list.map((item) {
      return makeAnchorHandle(item);
    }).toList();
  }

  @override
  Widget build(BuildContext context) {
    CallRecordsBlog blog = BlocProvider.of<CallRecordsBlog>(context);
    return StreamBuilder(
      stream: blog.callRecordsStream,
      initialData: blog.defaultData,
      builder: (
        BuildContext context,
        AsyncSnapshot snapshot,
      ) {
        List<CallRecordModel> list = snapshot.data;
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(),
          );
        } else {
          if (list.isEmpty) {
            return NoData("暂无通话记录");
          } else {
            return Container(
              color: Colors.white,
              child: EasyRefresh(
                header: EasyRefreshSetting.header,
                footer: EasyRefreshSetting.footer,
                onRefresh: () async {
                  blog.onRefresh();
                },
                onLoad: () async {
                  blog.getCallRecordLists();
                },
                child: ListView(
                  children: callList(list),
                ),
              ),
            );
          }
        }
      },
    );
  }
}
